package com.kai.Controller;

import com.kai.pojo.User;
import com.kai.service.UserService;
import com.kai.service.impl.UserServiceImpl;
import com.kai.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;


@Component
public class Register {

    @Autowired
    UserService userService;
    public void registered() {

        Scanner sc = new Scanner(System.in);

        while (true) {

            //用户名
            System.out.println("请输入用户名");
            String username = sc.next();
            //检查用户名是否存在
            if (userService.findByUsername(username)) {
                System.out.println("用户名已存在");
                continue;
            }

            //密码
            System.out.println("请输入密码");
            String password = sc.next();
            System.out.println("请再次输入密码");
            String password2 = sc.next();
            if (!password.equals(password2)) {
                System.out.println("两次密码不一致");
                continue;
            }

            //手机号
            System.out.println("请输入手机号");
            String phoneNumber = sc.next();
            if (!phoneNumber.matches("(?:(?:\\+|00)86)?1(?:3\\d|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8\\d|9[189])\\d{8}")) {
                System.out.println("手机号格式错误");
                continue;
            }

            //手机号
            System.out.println("请输入身份证号");
            String idCard = sc.next();
            if (!idCard.matches("[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]")) {
                System.out.println("身份证号格式错误");
                continue;
            }

            //验证码
            String code = CodeUtil.getCode();
            System.out.println(code);
            System.out.println("请输入验证码");
            if (!code.equalsIgnoreCase(sc.next())) {
                System.out.println("验证码错误");
                continue;
            }

            //加入数据库
            User user = new User(null, username, password, phoneNumber, idCard, null, null);
            userService.registered(user);

            System.out.println("注册成功");
            break;
        }




    }











}

